<template>
	<view class="container">
		<view class="lineData">
			<scroll-view scroll-x="true" scroll="scroll">
				<view class="columnTitWrap">
					<view class="columnTit">
						<!-- <text>校对标记1</text> -->
						<text>物料代码</text>
						<!-- <text>实发数量</text> -->
						<text>应发数量</text>
						<text>规格</text>
						<text>名称</text>
						<text>仓库</text>
						<text>仓位</text>
						<text>单位</text>
					</view>
				</view>
				<view :class="{'lineItem': true, 'FSign': item.FSign ===  'Y'}" v-for="(item, idx) in lineData" :key="idx">
					<!-- <text>{{ item.FSign}}</text> -->
						<text>{{ item.FNumber}}</text>
						<!-- <text>{{ item.FQty}}</text> -->
						<text>{{ item.FAuxQty}}</text>
						<text>{{ item.FModel }}</text>
						<text>{{ item.FName}}</text>
						<text>{{ item.FStock }}</text>
						<text>{{ item.FSP}}</text>
						<text>{{ item.FUnit}}</text>
				</view>
				
			</scroll-view>
		</view>
		<view style="clear: both;"></view>
		<neil-modal :show="show" :auto-close="false" @close="closeModal" @confirm="confirmUpdate" title="实发数量" confirm-text="确认" cancel-text="取消">
		    <view style="min-height: 90upx;padding: 32upx 24upx;">
				
		        <view>当前物料代码：{{ scanIdx >= 0 ? lineData[scanIdx].FNumber : '' }}</view>
		        <input class="uni-input" style="margin-top: 15px;border-bottom: 1px solid #dddddd;padding-bottom: 15px;" v-model="amount" placeholder="请输入" />
		    </view>
		</neil-modal>
		<button class="submitBt" :loading="loading" @click="$debounce(submit)">提 交</button>
		<scan-code></scan-code>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import scanCode from "@/components/scan-code/scan-code.vue"
	import neilModal from '@/components/neil-modal/neil-modal.vue';
	import { combineRequsetData } from '../../utils/util.js'
	import { mainUrl } from '../../utils/url.js'
	import {  mapState,  mapMutations } from 'vuex'
	
	export default {
		data() {
			return {
				scanIdx: -1,
				FJson: [],
				lineData: [],
				x: 0,
				y: 200,
				loading: false,
				show: false,
				amount: ''
			}
		},
		components: {
			uniPopup,
			scanCode,
			neilModal
		},
		computed: {
			...mapState(['fuserno'])  
		},
		onLoad(options) {
			this.FJson = JSON.parse(options.data)
			this.getLineData()
		},
		onShow () {
			var _this = this
			uni.$off('scancodedate')  
			uni.$on('scancodedate',(data) => {  
				_this.broadcastBackInfo(data.code)
			})
		},
		methods: {
			broadcastBackInfo (result) {
				let filterArr = this.lineData.filter((item, idx) => {
					if (item.FItemID == result.split('[')[0]) {
						this.scanIdx = idx
						return item
					}
				})
				if(!filterArr.length) {
					uni.showModal({
						content: '不存在此物料',
						showCancel: false
					})
				} else {
					this.lineData.map((item, idx) => {
						if (item.FItemID == result.split('[')[0]) {
							item.FSign = 'Y'
							return item
						}
					})
					// this.lineData[this.scanIdx].FSign = 'Y'
					// this.show = true
					// this.amount = this.lineData[this.scanIdx].FAuxQty
					// uni.showModal({
					//     title: '提示',
					//     content: '当前物料代码：' + this.lineData[this.scanIdx].FNumber + '\n' + '当前应发数量：' + this.lineData[this.scanIdx].FAuxQty,
					// 	editable: true,
					// 	placeholderText: '请输入实发数量',
					//     success: (res) => {
					//         if (res.confirm) {
					// 			if (res.content > this.lineData[this.scanIdx].FAuxQty || res.content == 0 || res.content.trim() == '') {
					// 				uni.showModal({
					// 					content: '实发数量应大于0并且小于应发数量!',
					// 					showCancel: false
					// 				})
					// 			} else {
					// 				this.lineData[this.scanIdx].FQty = res.content
					// 				// this.lineData[this.scanIdx].FSign = 'Y'
					// 			}
					//         } else if (res.cancel) {
					// 			this.scanIdx = null
					//         }
					//     }
					// });
				}

			},
			confirmUpdate () {
				if (this.amount > this.lineData[this.scanIdx].FAuxQty || this.amount == 0 || this.amount == '') {
					uni.showModal({
						content: '实发数量应大于0并且小于应发数量!',
						showCancel: false
					})
				} else {
					this.lineData[this.scanIdx].FQty =  this.amount
				}
			},
			checkSign (item) {
				// return item.FQty == 0
				return item.FSign != 'Y'
			},
			closeModal () {
				this.show = false
			},
			submit () {
				let filterArr = this.lineData.filter(this.checkSign)
				if (filterArr.length > 0) {
					uni.showModal({
						content: '当前还存在未校验的单子!',
						showCancel: false
					})
					return false
				}
				this.loading = true
				var tmpData = '<FJson>' + JSON.stringify({items: this.lineData}) + '</FJson>'
					tmpData += '<FTranType><![CDATA[' + this.FJson[0].FTranType + ']]></FTranType>'
					tmpData += '<fuserno>' + this.fuserno + '</fuserno>'
				uni.request({
					url: mainUrl,
					method: 'POST',
					data: combineRequsetData('check_21', tmpData),
					header:{
						'Content-Type':'text/xml; charset=utf-8'
					},
					success: (res) => {
						switch (res.data[0].code) {
							case '1':
								uni.showToast({
									title: '提交成功!',
									icon: 'success',
									mask: true,
									duration: 1500
								})
								setTimeout(() => {uni.navigateBack()}, 1500)
							break
							default:
								uni.showModal({
									content: '提交失败!',
									showCancel: false
								});
							break
						}
						
					},
					fail: (err) => {
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						});
					},
					complete: () => {
						this.loading = false
					}
				})
			},
			getLineData () {
				var tmpData = "<FSQL><![CDATA[select * from Z_ICStockBill_21_41Detail where FInterID='" + this.FJson[0].FInterID +"' ]]></FSQL>"
				// var tmpData = '<FJson>' + JSON.stringify({items: this.FJson}) +'</FJson>'
				uni.request({
					url: mainUrl,
					method: 'POST',
					data: combineRequsetData('JA_LIST', tmpData),
					header:{
						'Content-Type':'text/xml'
					},
					success: (res) => {
						console.log(res)
						this.lineData = res.data
					},
					fail: (err) => {
						console.log('request fail', err);
					},
					complete: () => {
						this.loading = false;
						uni.stopPullDownRefresh();
					}
				})
			}
		}
	}
</script>

<style>
	.container {
		padding: 20 0 20px 0;
		font-size: 14px;
		line-height: 24px;
	}
	.dbitem{
		width: 100%;
		padding: 10px 0;
		background-color: #ffffff;
		float: left;
	}
	.itemBar{
		width: calc(100% - 20px);
		height: 30px;
		line-height: 30px;
		padding: 0 10px;
		float: left;
	}
	.itemBar view:nth-of-type(1){
		float: left;
		width: 32%;
		color: #333333;
		font-weight: 500;
	}
	.itemBar view:nth-of-type(2){
		float: left;
		width: 68%;
		color: #999999;
		text-align: right;
	}
	.lineData{
		position: relative;
		/* z-index: 998; */
	}
	.columnTitWrap{
		padding: 0 5px;
		background: #C0C0C0;
	}
	.columnTit{
		width: 870px;
		height: 30px;
		background: #C0C0C0;
	}
	.columnTit text{
		width: 200px;
		height: 30px;
		/* padding: 0 10px; */
		line-height: 30px;
		display: inline-block;
		font-weight: 400;
	}
	.columnTit text:nth-of-type(1){
		width: 150px;
	}
	.columnTit text:nth-of-type(2){
		width: 80px;
	}
	.columnTit text:nth-of-type(5){
		width: 80px;
	}
	.columnTit text:nth-of-type(6){
		width: 80px;
	}
	.columnTit text:nth-of-type(7){
		width: 80px;
	}
	.lineItem{
		min-width: 870px;
		margin-bottom: 10px;
		display: flex;
		align-items: center;
		border-bottom: 1px dashed #555555;
	}
	.FSign{
		/* background-color: #dddddd; */
		background-color: green;
	}
	.lineItem text{
		width: 200px;
		/* padding: 0 10px; */
		display: inline-block;
		font-weight: 400;
	}
	.lineItem text:nth-of-type(1){
		padding-left: 5px;
		width: 150px;
	}
	.lineItem text:nth-of-type(2){
		width: 80px;
	}
	.lineItem text:nth-of-type(5){
		width: 80px;
	}
	.lineItem text:nth-of-type(6){
		width: 80px;
	}
	.lineItem text:nth-of-type(7){
		width: 80px;
	}
	.dotScan {
		width: 50px;
		height: 50px;
		border-radius: 50%;
		background: #1196DB;
		font-size: 14px;
		color: #ffffff;
		text-align: center;
		line-height: 50px;
		z-index: 999;
	}
	.submitBt{
		width: 100%;
		height: 50px;
		/* position: fixed;
		bottom: 0; */
		z-index: 999;
		color: #ffffff;
		background: #FF0000;
		border: 0;
		border-radius: 0;
	}
</style>
